package com.custom.action.core.methods.select;

import com.custom.action.core.methods.MethodKind;
import com.custom.action.joiner.JoinConditionWrapper;
import com.custom.comm.exceptions.CustomCheckException;
import com.custom.comm.page.DbPageRows;
import com.custom.jdbc.session.JdbcSqlSessionFactory;

/**
 * @author Xiao-Bai
 * @since 2023/5/17 22:27
 */
@SuppressWarnings("unchecked")
public class SelectPageJoinByWrapper extends SelectListJoinByWrapper {

    @Override
    public <T> Object doExecute(JdbcSqlSessionFactory sqlSessionFactory, Class<T> target, Object[] params) throws Exception {
        JoinConditionWrapper<T> wrapper = (JoinConditionWrapper<T>) params[0];
        if (!wrapper.hasPageParams()) {
            throw new CustomCheckException("Missing paging parameter：pageIndex：%s, pageSize：%s",
                    wrapper.getPageIndex(), wrapper.getPageSize()
            );
        }
        String selectSql = joinWrapperTargetSql(sqlSessionFactory, wrapper, target);
        DbPageRows<T> pageRows = new DbPageRows<>(wrapper.getPageIndex(), wrapper.getPageSize());
        super.buildPageResult(sqlSessionFactory, target, target, selectSql, pageRows, wrapper.getParamValues().toArray());
        return pageRows;
    }

    @Override
    public MethodKind getKind() {
        return MethodKind.SELECT_PAGE_JOIN_BY_WRAPPER;
    }
}
